package com.jay.demo.echo;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;

import java.nio.charset.Charset;

public class EchoClientHandler extends ChannelInboundHandlerAdapter {


    /**
      * Creates a client-side handler.
       */
      public EchoClientHandler() {
      }

      @Override
      public void channelActive(ChannelHandlerContext ctx) {
          ctx.writeAndFlush(Unpooled.copiedBuffer("hello world", CharsetUtil.UTF_8));
      }

      @Override
      public void channelRead(ChannelHandlerContext ctx, Object msg) {
          ByteBuf in = (ByteBuf) msg;
          System.out.println("Client receied: " + in.toString(CharsetUtil.UTF_8));
      }

     @Override
      public void channelReadComplete(ChannelHandlerContext ctx) {
         ctx.flush();
      }

     @Override
     public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {         // Close the connection when an exception is raised.
          cause.printStackTrace();
         ctx.close();
      }
  }
